package top.yokey.android.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import top.yokey.android.R;
import top.yokey.android.http.CircleBean;

public class CircleCommitAdapter extends RecyclerView.Adapter<CircleCommitAdapter.Holder> {

    private List<CircleBean.CommitDataDTO> arrayList;
    private OnItemClickListener onItemClickListener = null;

    public CircleCommitAdapter(List<CircleBean.CommitDataDTO> arrayList) {
        this.arrayList = arrayList;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_circle_commit, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {

        CircleBean.CommitDataDTO bean = arrayList.get(position);
        holder.nicknameTextView.setText(bean.getMemberNickname());
        if (bean.getReplyId() != -1) {
            holder.nicknameTextView.append(" 回复 " + bean.getReplyMemberNickname());
        }
        holder.nicknameTextView.append("：" + bean.getCommitContent());

        holder.nicknameTextView.setOnClickListener(view -> {
            if (onItemClickListener != null) onItemClickListener.onClick(position, bean);
        });

        holder.nicknameTextView.setOnLongClickListener(view -> {
            if (onItemClickListener != null) onItemClickListener.onLongClick(position, bean);
            return false;
        });

    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    //列表项单击接口
    public interface OnItemClickListener {

        //列表项整体单击
        void onClick(int position, CircleBean.CommitDataDTO bean);

        //列表项整体长按
        void onLongClick(int position, CircleBean.CommitDataDTO bean);

    }

    public static class Holder extends RecyclerView.ViewHolder {

        private AppCompatTextView nicknameTextView;

        public Holder(@NonNull View view) {
            super(view);
            nicknameTextView = view.findViewById(R.id.nicknameTextView);
        }

    }

}
